﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.Windows.Ria;
using System.Windows.Ria.ApplicationServices;
using System.Windows.Ria.Data;
using System.Windows.Data;
using System.ComponentModel;
using AlexApp;

namespace ExpenseReport
{
    /// <summary>
    /// The base page for the application which contains a frame that is used for navigation.  The page
    /// has a collapased "Create Data" button that can be used to create sample data in a database.
    /// </summary>
    public partial class ExpenseReportApp : Page
    {
        List<Employee> employees;
        ExpenseData expenseDataContext;

        public ExpenseReportApp()
        {
            InitializeComponent();
        }


        private void CreateData_Click(object sender, RoutedEventArgs e)
        {
            expenseDataContext = new ExpenseData();
            expenseDataContext.Loaded += new EventHandler<LoadedDataEventArgs>(expenseDataContext_Loaded);
            expenseDataContext.LoadEmployees();

        }

        void expenseDataContext_Loaded(object sender, LoadedDataEventArgs e)
        {
            expenseDataContext.Loaded -= new EventHandler<LoadedDataEventArgs>(expenseDataContext_Loaded);
            CreateSampleData();
        }

        private void CreateSampleData()
        {
            List<String> states = new List<string>(new string[] { "Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan, Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia ", "Washington", "West Virginia", "Wisconsin", "Wyoming" });
            List<String> abbreviations = new List<string>(new string[] { "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY" });
            List<String> details = new List<string>(new string[] { "Rental Car", "Hotel Stay", "Gas", "Meals" });
            employees = expenseDataContext.Employees.ToList();

            int i = 0;
            Random r = new Random();
            foreach (string state in states)
            {
                ExpenseReport er = new ExpenseReport()
                {
                    id = states.IndexOf(state)+1,
                    Company = "Company" + state[0],
                    Created = new DateTime(r.Next(1900, 2008),
                                          r.Next(1, 12),
                                          r.Next(1, 28)),
                    Department = "Dept " + (i == 0 ? "A" : "B"),
                    Employee = employees[i],
                    Status = r.Next(0, 3),
                    Description = "Trip to " + state

                };
                foreach (string detail in details)
                {
                    ExpenseReportDetails erd = new ExpenseReportDetails()
                    {
                        expensereportid = states.IndexOf(state) + 1,
                        Amount = (Decimal)((r.NextDouble() + 0.1) * 800.0),
                        Category = detail,
                        City = "Big City",
                        State = abbreviations[states.IndexOf(state)],
                        ShortDescription = detail + " purchase",
                        LongDescription = detail + " expense during " + state + " trip.",
                        CreditCardExpense = i == 0 ? false : true,
                        DateIncurred = new DateTime(er.Created.Value.Year,
                                                    r.Next(1, er.Created.Value.Month),
                                                    r.Next(1, er.Created.Value.Day)),
                    };
                    expenseDataContext.ExpenseReportDetails.Add(erd);
                    er.ExpenseReportDetails.Add(erd);
                }
                expenseDataContext.ExpenseReports.Add(er);
                i = i == 0 ? 1 : 0;
            }
            expenseDataContext.Submitted += new EventHandler<SubmittedChangesEventArgs>(expenseDataContext_Submitted);
            expenseDataContext.SubmitChanges();
        }

        void expenseDataContext_Submitted(object sender, SubmittedChangesEventArgs e)
        {
            expenseDataContext.Submitted -= new EventHandler<SubmittedChangesEventArgs>(expenseDataContext_Submitted);
            if (e.EntitiesInError.Count() > 0)
            {
                throw new Exception();
            }
        }
    }
}
